package com.bocai.dahuatech.ui.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.AddressListBean;
import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.utils.SharePrefencesUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 收货地址详情
 */
public class AddressDetailActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.tv_addr_detail_name)
    TextView tvName;
    @Bind(R.id.tv_addr_detail_phone)
    TextView tvPhone;
    @Bind(R.id.tv_addr_detail_postcode)
    TextView tvPostcode;
    @Bind(R.id.tv_addr_detail_city)
    TextView tvCity;
    @Bind(R.id.tv_addr_detail_road)
    TextView tvRoad;
    @Bind(R.id.tv_addr_detail_delete)
    TextView tvDeleteAddr;
    @Bind(R.id.tv_addr_detail_setdafalt)
    TextView tvSetDefaltAddr;
    @Bind(R.id.ll_setdefaltaddr)
    LinearLayout llSetDefalt;
    private AddressListBean.ContentEntity data;
    @Override
    protected int contentView() {
        return R.layout.activity_address_detail;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.addrdetail);
        mToolbar.inflateMenu(R.menu.menu_addaddress_edit);
        data= (AddressListBean.ContentEntity) getIntent().getSerializableExtra("data");
        if(data.isFDefault())
        {
            llSetDefalt.setVisibility(View.GONE);

        }else{
            llSetDefalt.setVisibility(View.VISIBLE);
        }
        setDatas();
        initEvent();
    }

    private void setDatas() {
        tvName.setText(data.getFConsignee());
        tvPhone.setText(data.getFMobilePhone());
        tvPostcode.setText(data.getFZipCode());
        tvCity.setText(data.getPCAName());
        tvRoad.setText(data.getFAddress());

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(AddressDetailActivity.this, AddressEditActivity.class);
                intent.putExtra("data", data);
                startActivity(intent);
                return false;
            }
        });
        tvDeleteAddr.setOnClickListener(this);
        tvSetDefaltAddr.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_addr_detail_delete:
                //删除地址
                showDialog();

                break;
            case R.id.tv_addr_detail_setdafalt:
                //设置默认地址
                setDafaltAddr();
                break;
        }
    }

    private void showDialog() {
        View dialogView= LayoutInflater.from(this).inflate(R.layout.dialog_delete,null);
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setView(dialogView).show();
        TextView tiitle=(TextView)dialog.findViewById(R.id.tv_dialog_title);
        tiitle.setText(R.string.deleteaddresssure);
        dialog.findViewById(R.id.tv_dialog_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteAddr();
            }
        });
        dialog.findViewById(R.id.tv_dialog_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    }

    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {
            Log.e("error","complete");
        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            showToast(baseBean.getReturnInfo());
            if(baseBean.getReturnNo().equals("1")){
                AddressDetailActivity.this.finish();
            }
        }
    };
    private void setDafaltAddr() {
        getTestApi().setDefaltAddr(SharePrefencesUtil.getCustomerCode(this),data.getFID())
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    private void deleteAddr() {
        getTestApi().deleteAddr(SharePrefencesUtil.getCustomerCode(this), data.getFID())
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
